<?php
//Include database connection
include("config.php");

//Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

//Information sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$repeatpassword=$_POST['repeatpassword'];
$bandbreedte=$_POST['bandbreedte'];
$gebruikers=$_POST['bandbreedte'];
$authenticatie=$_POST['authenticatie'];
$domein=$_POST['domein'];
$groepen=$_POST['groepen'];
$administratie=$_POST['administratie'];

//Check if passwords match
if($mypassword != $repeatpassword){
    echo "Passwords do not match. Please type again.";
    exit();
}

//Encrypt password
$encrypted_mypassword=MD5($mypassword);

//Check if username/password is empty
if(empty($myusername) || empty($mypassword)) die('Type username and password.');

//Protect MySQL injection
$myusername = stripslashes($myusername);
$encrypted_mypassword = stripslashes($encrypted_mypassword);
$myusername = mysql_real_escape_string($myusername);
$encrypted_mypassword = mysql_real_escape_string($encrypted_mypassword);

//Check if username already exists
$checkuser = mysql_query("SELECT * FROM members WHERE username='$myusername'");
$username_exist = mysql_num_rows($checkuser);

if($username_exist > 0){
    echo "I'm sorry but the username you specified has already been taken.  Please pick another one.";
    unset($username);
    exit();
}

//Create new user
$query = "INSERT INTO members (username, password, bandbreedte, gebruikers, authenticatie, domein, groepen, administratie) VALUES('$myusername', '$encrypted_mypassword','$bandbreedte', '$gebruikers', '$authenticatie', '$domein', '$groepen','$administratie')";
mysql_query($query) or die(mysql_error());
mysql_close();
echo "You have successfully registered";
?>
